.PHONY: build test clean install example vscode-package

# 变量定义
BINARY_NAME=goxc
BUILD_DIR=build
EXAMPLE_DIR=examples
VSCODE_DIR=vscode-extension

# 默认目标
all: build

# 构建编译器
build:
	@echo "构建 GoX 编译器..."
	@mkdir -p $(BUILD_DIR)
	go build -o $(BUILD_DIR)/$(BINARY_NAME) ./cmd/goxc
	@echo "构建完成: $(BUILD_DIR)/$(BINARY_NAME)"

# 构建语言服务器
build-lsp:
	@echo "构建 GoX 语言服务器..."
	@mkdir -p $(BUILD_DIR)
	go build -o $(BUILD_DIR)/gox-lsp ./cmd/gox-lsp
	@echo "构建完成: $(BUILD_DIR)/gox-lsp"

# 构建所有工具
build-all: build build-lsp

# 安装编译器到 GOPATH/bin
install:
	@echo "安装 GoX 编译器..."
	go install ./cmd/goxc
	@echo "安装完成"

# 安装语言服务器
install-lsp:
	@echo "安装 GoX 语言服务器..."
	go install ./cmd/gox-lsp
	@echo "安装完成"

# 安装所有工具
install-all: install install-lsp

# 运行测试
test:
	@echo "运行测试..."
	go test -v ./...
	@echo "测试完成"

# 清理构建产物
clean:
	@echo "清理构建产物..."
	rm -rf $(BUILD_DIR)
	rm -f $(EXAMPLE_DIR)/*.go
	@echo "清理完成"

# 编译示例文件
example: build
	@echo "编译示例文件..."
	./$(BUILD_DIR)/$(BINARY_NAME) -i $(EXAMPLE_DIR)/simple_example.gox -o $(EXAMPLE_DIR)/simple_example.go -v
	@echo "示例编译完成"

# 运行示例
run-example: example
	@echo "运行示例..."
	cd $(EXAMPLE_DIR) && go run simple_example.go

# 打包 VSCode 扩展
vscode-package:
	@echo "打包 VSCode 扩展..."
	cd $(VSCODE_DIR) && npm install && npm run compile
	@echo "VSCode 扩展打包完成"

# 开发模式：监听文件变化并重新构建
dev:
	@echo "开发模式启动..."
	@while true; do \
		inotifywait -r -e modify,create,delete ./pkg ./cmd 2>/dev/null; \
		echo "检测到文件变化，重新构建..."; \
		make build; \
	done

# 格式化代码
fmt:
	@echo "格式化代码..."
	go fmt ./...
	@echo "代码格式化完成"

# 代码检查
lint:
	@echo "代码检查..."
	golangci-lint run
	@echo "代码检查完成"

# 生成文档
docs:
	@echo "生成文档..."
	godoc -http=:6060
	@echo "文档服务器启动在 http://localhost:6060"

# 创建发布版本
release: clean test build
	@echo "创建发布版本..."
	@mkdir -p dist
	@tar -czf dist/gox-$(shell git describe --tags --always).tar.gz -C $(BUILD_DIR) $(BINARY_NAME)
	@echo "发布版本创建完成"

# 帮助信息
help:
	@echo "GoX 项目构建工具"
	@echo ""
	@echo "可用命令:"
	@echo "  build         构建 GoX 编译器"
	@echo "  install       安装编译器到 GOPATH/bin"
	@echo "  test          运行测试"
	@echo "  clean         清理构建产物"
	@echo "  example       编译示例文件"
	@echo "  run-example   编译并运行示例"
	@echo "  vscode-package 打包 VSCode 扩展"
	@echo "  dev           开发模式（监听文件变化）"
	@echo "  fmt           格式化代码"
	@echo "  lint          代码检查"
	@echo "  docs          启动文档服务器"
	@echo "  release       创建发布版本"
	@echo "  help          显示此帮助信息" 